﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using System.Data;

namespace RC1.teacher
{
    public partial class viewClass : System.Web.UI.Page
    {
        int class_id;
        int attendingCount;

        protected void Page_Load(object sender, EventArgs e)
        {

            class_id = Convert.ToInt32(Request.QueryString["id"]);
            Session["class_id"] = class_id;
            attendingCount = getAttendingCount();
            if (!Page.IsPostBack)
            {
                showClassInfo();
            }
        }

        private void showStudents()
        {

        }

        private void addCustomFields()
        {
            foreach (DataRow row in GridView1.Rows)
            {
                Label l = new Label();
                l.Text = "TEST";
            }
        }

        private void showClassInfo()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            
            var result = (from r in db.classes
                          join ui in db.user_info on r.user_id equals ui.user_id
                          where r.class_id == class_id
                          select new
                          {
                              name = ui.user_info_first_name + " " + ui.user_info_last_name,
                              fak = r.class_name,
                              description = r.class_description
                          }).SingleOrDefault();
            if (result != null)
            {
                Label1.Text = result.name;
                Label2.Text = result.fak;
                Label3.Text = result.description;
                Label4.Text = "" + attendingCount;
            }
        }

        private int getAttendingCount()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var attending = (from att in db.attendings
                             where att.class_id == class_id
                             select att);

            return attending.Count();
        }

        protected String getHandedInCount(int id)
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var handedIn = (from s in db.submissions
                             where s.assignment_id == id
                             select s);

            int handedInCount = handedIn.Count();

            return handedInCount + "/" + attendingCount;
        }

        protected String getTimeRemaining(int assignement_id)
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var assignment = (from a in db.assignments
                              where a.assignment_id == assignement_id
                              select a).SingleOrDefault();
            TimeSpan remaingin = assignment.assignment_deadline - DateTime.Now;
            if (DateTime.Now > assignment.assignment_deadline)
            {
                return "<span style='color:red;'>Freistin er farin</span>";
            }
            return remaingin.Days + " dagar, " + remaingin.Hours + ":" + remaingin.Minutes + ":" + remaingin.Seconds;
        }

        protected String getCorrected(int assignement_id)
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var corrected = (from a in db.assignments
                             join s in db.submissions on a.assignment_id equals s.assignment_id
                             join cs in db.corrected_submission on s.submission_id equals cs.submission_id
                             where a.assignment_id == assignement_id
                             select cs);
            int count = corrected.Count();
            return count + "/" + attendingCount;
        }

        protected String getButtons(int assignement_id)
        {
            String temp = "<a href='edit_assignment.aspx?id=" + assignement_id + "&p=" + class_id + "'><img src='../images/edit-icon.png' width='20' height='20' alt='Edit'/></a>";
            return temp;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["class_id"] = class_id;
            Response.Redirect("add_assignment.aspx");
        }

        protected void GridviewCommandEventHandler(Object sender, GridViewCommandEventArgs args)
        {
            if (args.CommandName.Equals("viewAssignment"))
            {
                String assignemntID = GridView1.DataKeys[Convert.ToInt32(args.CommandArgument)].Value.ToString();
                Session["assignment_id"] = assignemntID;
                Response.Redirect("view_assignment.aspx");
            }
        }

    }
}